<?php
/**
 * 数据库操作类
 * User: zyw
 * Date: 16/5/12
 * Time: 下午3:22
 */
class video_db{

    public static $connect = null;

    private function __construct(){
        return false;
    }
    /**
     * 数据库链接
     * @return PDO
     */
    private static function conn(){
        $pdo = new PDO('mysql:host=39.104.100.139;dbname=short_video', 'short_video', 'shortVideo_123');
        //$pdo = new PDO('mysql:host=192.168.1.100;dbname=short_video', 'short_video', 'shortVideo_123');
        $pdo->query('set names utf8mb4');
        return $pdo;
    }

    /**
     * 获取数据链接
     * @return PDO
     */
    public static function getdb(){
        if(self::$connect == null){
            self::$connect = self::conn();
        }
        return self::$connect;
    }

    /**
     * @param $sql
     * @param array $param
     * @return mixed
     */
    public static  function fetch($sql,$param=array()){
        $pdo = self::getdb();
        $ret = $pdo->prepare($sql);
        $ret -> execute($param);
        return $ret->fetch(PDO::FETCH_ASSOC);
    }

    /**
     * @param $sql
     * @param array $param
     * @return array
     */
    public static function fetchAll($sql,$param=array()){
        $pdo = self::getdb();
        $ret = $pdo->prepare($sql);
        $ret->execute($param);
        return $ret->fetchAll(PDO::FETCH_ASSOC);
    }

    /**
     * @param $sql
     * @param array $param
     * @return bool
     */
    public static  function execute($sql,$param=array()){
        $pdo = self::getdb();
        $ret = $pdo->prepare($sql);
        @$ret->execute($param);
        return $pdo->lastInsertId();
    }

    public static function close(){
        self::$connect = null;
    }
}